/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

/*
 * Sources:
 * http://developer.android.com/guide/topics/ui/menus.html
 * http://developer.android.com/reference/android/preference/PreferenceFragment.html
 */

package com.gameoflife;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

/** Display the preferences for the application. */
public class PreferencesActivity extends PreferenceActivity {

    private static final String OPTION_MINIMUM = "UNDERPOPULATION_VARIABLE";
    private static final String OPTION_MINIMUM_DEFAULT = "2";
    private static final String OPTION_MAXIMUM = "OVERPOPULATION_VARIABLE";
    private static final String OPTION_MAXIMUM_DEFAULT = "3";
    private static final String OPTION_SPAWN = "SPAWN_VARIABLE";
    private static final String OPTION_SPAWN_DEFAULT = "3";
    private static final String OPTION_ANIMATION_SPEED = "ANIMATION_SPEED_VARIABLE";
    private static final String OPTION_ANIMATION_SPEED_DEFAULT = "3";
    
    // constants for color coding
    private static final String OPTION_COLOR_CODING = "COLOR_CODING";
    private static final Boolean OPTION_COLOR_CODING_DEFAULT = false;
    
    public PreferencesActivity()
	{
		MainActivity.activities.add(this);
	}
    
    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }
    
    /** Preference fragment for game settings. */
    public static class Prefs1Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);
        }
    }
    
    /** Preference fragment for game look and feel. */
    public static class Prefs2Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.lookandfeel);
        }
    }
    
    /* Properties */
    public static String getMinimumVariable(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
            getString(OPTION_MINIMUM, OPTION_MINIMUM_DEFAULT);
    }
    
    public static String getMaximumVariable(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
            getString(OPTION_MAXIMUM, OPTION_MAXIMUM_DEFAULT);
    }
    
    public static String getSpawnVariable(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
            getString(OPTION_SPAWN, OPTION_SPAWN_DEFAULT);
    }
    
    public static String getAnimationSpeed(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
            getString(OPTION_ANIMATION_SPEED, OPTION_ANIMATION_SPEED_DEFAULT);
    }
    
    public static Boolean getColorCoding(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).
            getBoolean(OPTION_COLOR_CODING, OPTION_COLOR_CODING_DEFAULT);
    }
}